const { ObjectId } = require('mongodb')
const mongoose = require('mongoose')
const express = require('express')
const Task = mongoose.model('Task')
const User = mongoose.model('User')
const router = express.Router()

router.get('/json', async (req, res) => {
  const task = await Task.findOne({
    $and: [{ pass: false }, { user: { $exists: true } }],
  })
    .sort({ createdAt: 1 })
    .limit(1)
  let data
  if (task) {
    data = {
      id: task._id,
      input: task.name,
      style: task.style,
      user: task.user,
    }
  }

  res.send({
    status: true,
    message: 'ok',
    data,
  })
})

router.get('/', async (req, res) => {
  let data = []
  let query = {}
  const pageSize = Number(req.query.size || 30)
  let page  = Number(req.query.page || 1)
  const skip = (page - 1) * pageSize

  if (req.query.user) {
    query.user = new ObjectId(req.query.user)
  }
  if (req.query.pass) {
    query.pass = req.query.pass
  }  
  data = await Task.find(query).populate('user').skip(skip).sort({ createdAt: -1 }).limit(pageSize)

  const list = data.map((e, i) => {
    const jsonObject = Object.assign({}, e._doc)
    if (e.user) {
      jsonObject.user = e.user._id
      jsonObject.username = e.user.name
    }
    return jsonObject
  })
  res.send({
    status: true,
    message: 'ok',
    data: list,
  })
})

router.delete('/', async (req, res) => {
  const data = await Task.findOne({
    _id: new ObjectId(req.query.id),
    user: new ObjectId(req.query.user),
  })
  if (data) {
    if (data.pass) {
      res.send({
        status: false,
        message: '不能删除已经开始',
      })
    } else {
      data.delete()
      res.send({
        status: true,
        message: 'ok',
      })
    }
  } else {
    res.send({
      status: false,
      message: '没找到要删除的任务',
    })
  }
})

router.post('/', async (req, res) => {
  try {
    const task = await Task.create(req.body)
    res.send({
      status: true,
      message: 'Task succeeded ',
      data: {
        id: task._id,
        name: task.name,
      },
    })
  } catch (err) {
    res.status(500).send(err)
  }
})

router.put('/', async (req, res) => {
  try {
    // product
    // console.log(req.body);
    const task = await Task.findOne({ _id: req.body.id })
    if (task) {
      if (req.body.product) {
        task.product = req.body.product
      }
      if (req.body.progress) {
        task.progress = req.body.progress
      }
      task.pass = true
      task.save()
    }
    res.send({
      status: true,
      message: 'Task succeeded ',
      data: {
        id: task._id,
        name: task.name,
        progress: task.progress,
        product: task.product,
        pass: task.pass,
      },
    })
  } catch (err) {
    console.log(err)
    res.status(500).send(err)
  }
})

router.put('/like', async (req, res) => {
  try {
    const task = await Task.findOne({ _id: req.body.id })
    if (task) {
      task.like++
      task.save()
    }
    const user = await User.findOne({ _id: req.body.user })
    if (user) {
      user.like++
      user.save()
    }
    res.send({
      status: true,
      message: 'Task succeeded ',
      data: {
        id: task._id,
        like: task.like,
        userlike: user.like,
      },
    })
  } catch (err) {
    console.log(err)
    res.status(500).send(err)
  }
})

module.exports = router
